package com.tool.base.common;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

import com.tool.base.utils.DesUtils;


public class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {

	private static final String ENCRYPT_PROPERTY_PREFIX_SHA1PRNG = "{SHA1PRNG}";

	@Override
	protected String convertProperty(String propertyName, String propertyValue) {
		// 如果属性值为SHA1PRNG加密
		if (isSHA1PRNG(propertyValue)) {
			return DesUtils.getDecryptString(
					propertyValue.substring(ENCRYPT_PROPERTY_PREFIX_SHA1PRNG.length(), propertyValue.length()));
		} else {
			return propertyValue;
		}
	}

	private boolean isSHA1PRNG(String propertyValue) {
		if (StringUtils.isNotBlank(propertyValue) && propertyValue.startsWith(ENCRYPT_PROPERTY_PREFIX_SHA1PRNG)) {
			return true;
		}
		return false;
	}

}
